/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var mergeTwoLists = function(l1, l2) {
  var prevHead = new ListNode(-1);
  var prevNode = prevHead;
  while(l1 != null && l2 != null) {
    let lv1 = l1.val;
    let lv2 = l2.val;
    console.log(lv1, lv2)
    if(lv1 > lv2) {
      prevNode.next = l2;
      l2 = l2.next;
     } else {
      prevNode.next = l1;
      l1 = l1.next;
    }
    prevNode = prevNode.next;
  }
  prevNode.next = l1 || l2;
  return prevHead.next;
};
function ListNode(val, next) {
  this.val = val;
  this.next = next === undefined ? null : next;
}

function createNode(l) {
  let link = new ListNode();
  l.reduce((prev, next) => {
    prev.next = new ListNode(next)
    return prev.next;
  }, link);
  return link.next;
}

mergeTwoLists(createNode([0, 1,2,4]), createNode([1,3,4, 5]));